<?php

namespace app\circle\controller\admin;

use app\circle\model\CompanyErrorModel;
use app\circle\model\CompanyModel;
use app\circle\model\DeptModel;
use think\Request;

class CompanyController extends BaseController
{

    private $companyModel;
    private $deptModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->companyModel = new CompanyModel();
        $this->deptModel = new DeptModel();
    }

    /**
     * 企业库首页
     * @return mixed
     */
    public function index()
    {
        $map          = [];
        $query        = [];
        if($this->adminInfo['admin_power']>1){
            $map['dept_id'] = $this->adminInfo['dept_id'];
        }
        $searchKey    = $this->request->param('searchKey');
        $searchText   = $this->request->param('searchText');
        if (!empty($searchKey) && !empty($searchText)) {
            $map[$searchKey]     = ['like', '%' . $searchText . '%'];
            $query['searchKey']  = $searchKey;
            $query['searchText'] = $searchText;
        }
        $comList = $this->companyModel->where($map)->paginate(12, false, ['query' => $query]);;
        $this->assign('comList',$comList);
        return $this->fetch();
    }

    /**
     * 添加企业
     */
    public function add()
    {
        if($this->request->isPost()){
            $data = $this->request->param();
            $data['dept_name'] = $this->deptModel->getTree()[$data['dept_id']]['dept_name'];
            $data['dept_path'] = $this->deptModel->getTree()[$data['dept_id']]['dept_path'];
            $data['admin_id']  = $this->adminInfo['admin_id'];
            $result = $this->validate($data,[
                'dept_id|所属部门' => 'gt:0',
                'company_name|公司名称'  => 'require',
                'company_no|公司编号'  => 'require',
            ]);
            if($result !== true){
                $this->error($result);
            }
            $data['is_examine']=1;
            $this->companyModel->addData($data);
            $this->success('添加成功！',url('index'));
        }
        $this->assign('deptTree',$this->deptModel->getTree());
        return $this->fetch();
    }

    /**
     * 修改企业
     * @return mixed
     */
    public function edit()
    {
        if($this->request->isPost()){
            $data = $this->request->param();
            $data['dept_name'] = $this->deptModel->getTree()[$data['dept_id']]['dept_name'];
            $data['dept_path'] = $this->deptModel->getTree()[$data['dept_id']]['dept_path'];
            $result = $this->validate($data,[
                'dept_id|所属部门' => 'gt:0',
                'company_name|公司名称'  => 'require',
                'company_no|公司编号'  => 'require',
            ]);
            if($result !== true){
                $this->error($result);
            }
            $this->companyModel->editData($data);
            $this->success('修改成功！',url('index'));
        }
        $companyId = $this->request->param('company_id');
        $info = $this->companyModel->where('company_id',$companyId)->find();
        $errModel=new CompanyErrorModel();
        $errList=$errModel->where('company_id',$companyId)->where('status',0)->order('create_time desc')->select();
        $this->assign('errList',$errList);
        $this->assign('deptTree',$this->deptModel->getTree());
        $this->assign('info',$info);
        return $this->fetch();
    }

    /**
     * 关闭企业
     * @return mixed
     */
    public function del()
    {
        $companyId=$this->request->param('company_id',0);
        if ($this->companyModel->destroy($companyId)){
            $this->success('删除成功','index');
        }else{
            $this->error('删除成功','index');
        }
    }

    public function setErrMsg()
    {
        $id=$this->request->param('error_id',0);
        $model=new CompanyErrorModel();
        if ($model->where('error_id',$id)->setField('status',1)){
            $this->success('已处理');
        }else{
            $this->error('请重试');
        }
    }

    public function examine()
    {
        $companyId=$this->request->param('company_id',0);
        if ($this->companyModel->where('company_id',$companyId)->setField('is_examine',1)){
            $this->success('审核通过');
        }else{
            $this->error('审核失败');
        }
    }
}
